package cn.ekuma.epos.qkorder;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.JTextField;

import cn.ekuma.data.dao.bean.IKeyed;
import cn.ekuma.epos.datalogic.I_DataLogicERP;

import com.openbravo.bean.Product;
import com.openbravo.bean.viewbean.Product_V;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.JImageEditor;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.event.ProductChangedListener;

import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.SwingConstants;

public class JProductDetail extends JPanel implements ProductChangedListener{
	
	protected I_DataLogicERP dlSales;
	
	private JTextField m_jTax;
	private JTextField m_jCategory;
	private JTextField jm_sManufacturer;
	private JTextField jm_similarCode;
	private JTextField jm_iDurability;
	private JTextField jm_origin;
	private JTextField m_jPriceSell;
	private JTextField m_jCustomerPrice;
	private JTextField m_jWholesalePrice;

	private String productID;

	private JImageEditor imageEditor;
	private JTextField m_jAtt;
	private JTextField jm_unit;
	private JTextField jm_quantity;
	
	

	public JProductDetail(I_DataLogicERP dlSales) {
		this.dlSales = dlSales;
		initComp();
	}

	/**
	 * Create the panel.
	 */
	private void  initComp() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(0, 1, 0, 0));
		
		JLabel label = new JLabel();
		panel.add(label);
		label.setText(AppLocal.getIntString("label.taxcategory"));
		
		m_jTax = new JTextField();
		m_jTax.setBackground(new Color(255, 255, 255));
		m_jTax.setDisabledTextColor(Color.LIGHT_GRAY);
		m_jTax.setForeground(Color.BLACK);
		m_jTax.setRequestFocusEnabled(false);
		m_jTax.setFocusable(false);
		m_jTax.setEditable(false);
		panel.add(m_jTax);
		m_jTax.setColumns(10);
		
		JLabel label_1 = new JLabel();
		panel.add(label_1);
		label_1.setText(AppLocal.getIntString("label.prodcategory"));
		
		m_jCategory = new JTextField();
		m_jCategory.setBackground(new Color(255, 255, 255));
		m_jCategory.setDisabledTextColor(Color.LIGHT_GRAY);
		m_jCategory.setForeground(Color.BLACK);
		m_jCategory.setRequestFocusEnabled(false);
		m_jCategory.setFocusable(false);
		m_jCategory.setEditable(false);
		panel.add(m_jCategory);
		m_jCategory.setColumns(10);
		
		JLabel label20 = new JLabel(AppLocal.getIntString("label.product.quantity"));
		panel.add(label20);
		
		jm_quantity = new JTextField();
		jm_quantity.setHorizontalAlignment(SwingConstants.CENTER);
		jm_quantity.setBackground(new Color(255, 255, 255));
		jm_quantity.setDisabledTextColor(Color.LIGHT_GRAY);
		jm_quantity.setForeground(Color.BLACK);
		jm_quantity.setRequestFocusEnabled(false);
		jm_quantity.setFocusable(false);
		jm_quantity.setEditable(false);
		panel.add(jm_quantity);
		jm_quantity.setColumns(10);
		
		JLabel label_2 = new JLabel();
		panel.add(label_2);
		label_2.setText(AppLocal.getIntString("label.prodcut.m_sManufacturer"));
		
		jm_sManufacturer = new JTextField();
		jm_sManufacturer.setHorizontalAlignment(SwingConstants.CENTER);
		jm_sManufacturer.setBackground(new Color(255, 255, 255));
		jm_sManufacturer.setDisabledTextColor(Color.LIGHT_GRAY);
		jm_sManufacturer.setForeground(Color.BLACK);
		jm_sManufacturer.setRequestFocusEnabled(false);
		jm_sManufacturer.setFocusable(false);
		jm_sManufacturer.setEditable(false);
		panel.add(jm_sManufacturer);
		jm_sManufacturer.setColumns(10);
		
		JLabel label_9 = new JLabel();
		label_9.setText(AppLocal.getIntString("label.attributes"));
		panel.add(label_9);
		
		m_jAtt = new JTextField();
		m_jAtt.setBackground(new Color(255, 255, 255));
		m_jAtt.setDisabledTextColor(Color.LIGHT_GRAY);
		m_jAtt.setForeground(Color.BLACK);
		m_jAtt.setRequestFocusEnabled(false);
		m_jAtt.setFocusable(false);
		m_jAtt.setEditable(false);
		panel.add(m_jAtt);
		m_jAtt.setColumns(10);
		
		JLabel label21 = new JLabel(AppLocal.getIntString("label.produnits"));
		panel.add(label21);
		
		jm_unit = new JTextField();
		jm_unit.setHorizontalAlignment(SwingConstants.CENTER);
		jm_unit.setBackground(new Color(255, 255, 255));
		jm_unit.setDisabledTextColor(Color.LIGHT_GRAY);
		jm_unit.setForeground(Color.BLACK);
		jm_unit.setRequestFocusEnabled(false);
		jm_unit.setFocusable(false);
		jm_unit.setEditable(false);
		panel.add(jm_unit);
		jm_unit.setColumns(10);
		
		JLabel label_3 = new JLabel();
		panel.add(label_3);
		label_3.setText(AppLocal.getIntString("label.product.m_similarCode"));
		
		jm_similarCode = new JTextField();
		jm_similarCode.setBackground(new Color(255, 255, 255));
		jm_similarCode.setDisabledTextColor(Color.LIGHT_GRAY);
		jm_similarCode.setForeground(Color.BLACK);
		jm_similarCode.setRequestFocusEnabled(false);
		jm_similarCode.setFocusable(false);
		jm_similarCode.setEditable(false);
		panel.add(jm_similarCode);
		jm_similarCode.setColumns(10);
		
		JLabel label_4 = new JLabel();
		panel.add(label_4);
		label_4.setText(AppLocal.getIntString("label.product.m_iDurability"));
		
		jm_iDurability = new JTextField();
		jm_iDurability.setHorizontalAlignment(SwingConstants.CENTER);
		jm_iDurability.setBackground(new Color(255, 255, 255));
		jm_iDurability.setDisabledTextColor(Color.LIGHT_GRAY);
		jm_iDurability.setForeground(Color.BLACK);
		jm_iDurability.setRequestFocusEnabled(false);
		jm_iDurability.setFocusable(false);
		jm_iDurability.setEditable(false);
		panel.add(jm_iDurability);
		jm_iDurability.setColumns(10);
		
		JLabel label_5 = new JLabel();
		panel.add(label_5);
		label_5.setText(AppLocal.getIntString("label.product.origin"));
		
		jm_origin = new JTextField();
		jm_origin.setHorizontalAlignment(SwingConstants.CENTER);
		jm_origin.setBackground(new Color(255, 255, 255));
		jm_origin.setDisabledTextColor(Color.LIGHT_GRAY);
		jm_origin.setForeground(Color.BLACK);
		jm_origin.setRequestFocusEnabled(false);
		jm_origin.setFocusable(false);
		jm_origin.setEditable(false);
		panel.add(jm_origin);
		jm_origin.setColumns(10);
		
		JLabel label_6 = new JLabel();
		panel.add(label_6);
		label_6.setText(AppLocal.getIntString("label.prodpricesell"));
		
		m_jPriceSell = new JTextField();
		m_jPriceSell.setHorizontalAlignment(SwingConstants.RIGHT);
		m_jPriceSell.setBackground(new Color(255, 255, 255));
		m_jPriceSell.setDisabledTextColor(Color.LIGHT_GRAY);
		m_jPriceSell.setForeground(Color.BLACK);
		m_jPriceSell.setRequestFocusEnabled(false);
		m_jPriceSell.setFocusable(false);
		m_jPriceSell.setEditable(false);
		panel.add(m_jPriceSell);
		m_jPriceSell.setColumns(10);
		
		JLabel label_7 = new JLabel();
		panel.add(label_7);
		label_7.setText(AppLocal.getIntString("label.baseCustomerPrice"));
		
		m_jCustomerPrice = new JTextField();
		m_jCustomerPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		m_jCustomerPrice.setBackground(new Color(255, 255, 255));
		m_jCustomerPrice.setDisabledTextColor(Color.LIGHT_GRAY);
		m_jCustomerPrice.setForeground(Color.BLACK);
		m_jCustomerPrice.setRequestFocusEnabled(false);
		m_jCustomerPrice.setFocusable(false);
		m_jCustomerPrice.setEditable(false);
		panel.add(m_jCustomerPrice);
		m_jCustomerPrice.setColumns(10);
		
		JLabel label_8 = new JLabel();
		panel.add(label_8);
		label_8.setText(AppLocal.getIntString("label.WholesalePrice"));
		
		m_jWholesalePrice = new JTextField();
		m_jWholesalePrice.setHorizontalAlignment(SwingConstants.RIGHT);
		m_jWholesalePrice.setBackground(new Color(255, 255, 255));
		m_jWholesalePrice.setDisabledTextColor(Color.LIGHT_GRAY);
		m_jWholesalePrice.setForeground(Color.BLACK);
		m_jWholesalePrice.setRequestFocusEnabled(false);
		m_jWholesalePrice.setFocusable(false);
		m_jWholesalePrice.setEditable(false);
		panel.add(m_jWholesalePrice);
		m_jWholesalePrice.setColumns(10);
		
	    imageEditor = new JImageEditor();
	    imageEditor.setRequestFocusEnabled(false);
	    imageEditor.setFocusable(false);
	    imageEditor.setEnabled(false);
		imageEditor.setPreferredSize(new Dimension(200, 200));
		add(imageEditor);

	}

	@Override
	public void baseBeanChanged(IKeyed productId, Product o) {
		if(!isProductIDChanged((String) productId.getKey())||!isVisible())
            return;
        if(productId==null)
        	updateUI(null);
        else{
        	try {
				updateUI(dlSales.findProduct_VByProductId((String) productId.getKey()));
				imageEditor.setImage(ImageUtils.readImage(dlSales.findProductImage((String) productId.getKey())));
			} catch (BasicException e) {
				e.printStackTrace();
				updateUI(null);
			}
        }
		
	}
	
	private void updateUI(Product_V obj){
		if(obj!=null){
		jm_origin.setText(Formats.STRING.formatValue(obj.getM_origin()));
        jm_quantity.setText(Formats.DOUBLE.formatValue(obj.getM_quantity()));
        jm_unit.setText(Formats.STRING.formatValue(obj.getM_unit()));
        jm_sManufacturer.setText(Formats.STRING.formatValue(obj.getM_sManufacturer()));
        jm_similarCode.setText(Formats.STRING.formatValue(obj.getM_sSimilarCode()));
        jm_iDurability.setText(Formats.INT.formatValue(obj.getM_iDurability()));
        m_jCategory.setText(obj.getCategory());
        m_jTax.setText(obj.getM_sTax());
    	//m_jPriceBuy.setText(Formats.CURRENCY.formatValue(obj.getPriceBuy()));
        m_jPriceSell.setText(Formats.CURRENCY.formatValue(obj.getM_dPriceSell()));
        m_jCustomerPrice.setText(Formats.CURRENCY.formatValue(obj.getM_dCustomerPrice()));
        m_jWholesalePrice.setText(Formats.CURRENCY.formatValue(obj.getM_dWholesalePrice()));  
        m_jAtt.setText(obj.getAttributeset());
		}else{
			jm_origin.setText(null);
	        jm_quantity.setText(null);
	        jm_unit.setText(null);
	        jm_sManufacturer.setText(null);
	        jm_similarCode.setText(null);
	        jm_iDurability.setText(null);
	        m_jCategory.setText(null);
	        m_jTax.setText(null);
	    	//m_jPriceBuy.setText(null);
	        m_jPriceSell.setText(null);
	        m_jCustomerPrice.setText(null);
	        m_jWholesalePrice.setText(null);  
	        m_jAtt.setText(null);
		}
	}

	
	protected boolean isProductIDChanged(String inProdcutID) {
	    if(inProdcutID==null){
	        if(productID!=null)
	            return true;
	    }else if(productID==null)
	            return true;
	      else if(!productID.equalsIgnoreCase(inProdcutID))
	            return true;
	    return false;
	}
}
